import { MockMethod } from 'vite-plugin-mock'
const baseUrl = '/viteApp/api/mock';

const allAddress = {
	addressList: [{
		addressId: '1',
		name: '张三',
		tel: '13000000000',
		province: "北京市",
		city: "北京市",
		county: "东城区",
		areaCode: "110101",
		addressDetail: '文三路138号东方通信大厦7楼501室',
		postalCode: "210234",
		isDefault: true,
	},
	{
		addressId: '2',
		name: '李四',
		tel: '1310000000',
		province: "北京市",
		city: "北京市",
		county: "东城区",
		areaCode: "110101",
		addressDetail: '莫干山路50号',
		postalCode: "210234",
		isDefault: false
	},
	{
		addressId: '3',
		name: '王五',
		tel: '1320000000',
		province: "北京市",
		city: "北京市",
		county: "东城区",
		areaCode: "110101",
		addressDetail: '江南大道 15 号',
		postalCode: "210234",
		isDefault: false
	},
	{
		addressId: '4',
		name: '金小姐',
		tel: '1330000000',
		province: "北京市",
		city: "北京市",
		county: "东城区",
		areaCode: "110101",
		addressDetail: '华南路123弄34号',
		postalCode: "210234",
		isDefault: false
	},
	{
		addressId: '5',
		name: '马先生',
		tel: '13412345678',
		province: "北京市",
		city: "北京市",
		county: "东城区",
		areaCode: "110101",
		addressDetail: '幸福小镇545号',
		postalCode: "210234",
		isDefault: false
	}],
	disabledList: [
		{
			addressId: '6',
			name: '范天亦',
			tel: '15812345678',
			province: "北京市",
			city: "北京市",
			county: "东城区",
			areaCode: "110101",
			addressDetail: '张扬路2134弄23号301室',
			postalCode: "210234",
			isDefault: false
		}, {
			addressId: '7',
			name: '范天亦',
			tel: '15812345678',
			province: "上海市",
			city: "上海市",
			county: "浦东新区",
			areaCode: "110101",
			addressDetail: '张扬路2134弄23号301室',
			postalCode: "210234",
			isDefault: false
		}
	]
}
function getAddressInfo(addressId: string, data: object) {
	let addressInfo = null;
	for (const [key, val] of Object.entries(data)) {
		let datalist = val
		for (let i = 0; i < datalist.length; i++) {
			if (addressId == datalist[i].addressId) {
				addressInfo = datalist[i]
			}
		}
	}
	return addressInfo
}
export default [
	{
		url: baseUrl + "/addressList",
		method: "get",
		response: () => {
			return {
				code: 0,
				message: "success",
				data: allAddress
			}
		}
	},
	{
		url: baseUrl + "/addAddress",
		method: "post",
		response: (e) => {
			const addressInfo = e.body
			if(addressInfo){
				return {
					code: 0,
					message: "success",
					data: "新增成功"
				}
			}else{
				return {
					code: 0,
					message: "success",
					data: "新增成功"
				}
			}
			
		}
	},
	{
		url: baseUrl + "/editAddress",
		method: "post",
		response: (e) => {
			const addressId =e.body.addressId
			if(addressId){
				return {
					code: 0,
					message: "success",
					data: "修改成功"
				}
			}else{
				return {
					code: 1,
					message: "error",
					data: "修改失败"
				}
			}
		}
	},
	{
		url: baseUrl + "/delAddress",
		method: "post",
		response: (e) => {
			const addressId =e.body.addressId
			if(addressId){
				return {
					code: 0,
					message: "success",
					data: "删除成功"
				}
			}else{
				return {
					code: 1,
					message: "error",
					data: "删除失败"
				}
			}
		
		}
	},
	{
		url: baseUrl + "/addressInfo",
		method: "get",
		response: (e) => {
			const params = e.query ? e.query : "";
			let { addressId } = params
			let data = getAddressInfo(addressId, allAddress)
			return {
				code: 0,
				message: "success",
				data: data
			}
		}
	}
] as MockMethod[]